﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class goods : BaseAdmin
    {
          // Fields

        DALProfessionAreaType theProfessionAreaType = new DALProfessionAreaType();

    // Methods
    private void Bind()
    {

        if ((base.Request.QueryString["pid"] != null) && (base.Request.QueryString["action"] != null))
        {
            if (base.Request.QueryString["action"].ToString() == "yes1")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisshow=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "no1")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisshow=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "yes2")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew1=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "no2")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew1=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "yes3")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew2=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "no3")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew2=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "yes4")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew3=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "no4")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew3=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "yes5")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew4=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "no5")
            {
                SQLAccess.DoSql("update DB_Shoppro set proisnew4=1 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "del")
            {
                SQLAccess.DoSql("update DB_Shoppro set visible=0 where proid=" + base.Request.QueryString["pid"].ToString() + "");
                base.Response.Write("<script language='javascript'>alert('更新成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
            if (base.Request.QueryString["action"].ToString() == "copy")
            {
                DataTable table2 = SQLAccess.GreatDs("select * from DB_Shoppro where proid=" + base.Request.QueryString["pid"] + "").Tables[0];
                if (table2.Rows.Count > 0)
                {
                    DataTable table3 = SQLAccess.GreatDs("select * from DB_Memberprice where productid=" + table2.Rows[0]["proid"].ToString() + "").Tables[0];
                    if (table3.Rows.Count > 0)
                    {
                        foreach (DataRow row3 in table3.Rows)
                        {
                            SQLAccess.DoSql("insert into DB_Memberprice (rankid,productid,price) values (" + row3["rankid"].ToString() + "," + re.maxid("", "DB_Shoppro", "proid") + "," + row3["price"].ToString() + ")");
                        }
                    }
                    SQLAccess.DoSql(string.Concat(new object[] { 
                        "insert into DB_Shoppro (proid,proname,procode,typeid,propinpai,proprice1,proprice2,proprice3,proscore1,proscore2,proprice4,iscuxao,prodate1,prodate2,propic1,propic2,procontent,proweight1,proweight2,proku1,proku2,proisnew1,proisnew2,proisnew3,proisshow,proisp,prokey,promsg,promark,prosize,procolor,visible,pronameen,procontenten,updatetime,languages,clicks,proorder) values (", re.maxid("", "DB_Shoppro", "proid"), ",'", re.replace(table2.Rows[0]["proname"].ToString()), "','", re.replace(table2.Rows[0]["procode"].ToString()), "',", table2.Rows[0]["typeid"].ToString(), ",", table2.Rows[0]["propinpai"].ToString(), ",", table2.Rows[0]["proprice1"].ToString(), ",", table2.Rows[0]["proprice2"].ToString(), ",", table2.Rows[0]["proprice3"].ToString(), 
                        ",", table2.Rows[0]["proscore1"].ToString(), ",", table2.Rows[0]["proscore2"].ToString(), ",", table2.Rows[0]["proprice4"].ToString(), ",", "'" + (bool) table2.Rows[0]["iscuxao"] +"'", ",'", re.replace(table2.Rows[0]["prodate1"].ToString()), "','", re.replace(table2.Rows[0]["prodate2"].ToString()), "','", re.replace(table2.Rows[0]["propic1"].ToString()), "','", re.replace(table2.Rows[0]["propic2"].ToString()), 
                        "','", re.replace(table2.Rows[0]["procontent"].ToString()), "','", re.replace(table2.Rows[0]["proweight1"].ToString()), "','", re.replace(table2.Rows[0]["proweight2"].ToString()), "',", table2.Rows[0]["proku1"].ToString(), ",", table2.Rows[0]["proku2"].ToString(), ",","'" + (bool) table2.Rows[0]["proisnew1"] + "'", ",", "'" + (bool) table2.Rows[0]["proisnew2"] + "'", ",", "'" + (bool) table2.Rows[0]["proisnew3"] + "'", 
                        ",", "'" + (bool) table2.Rows[0]["proisshow"] + "'", ",", "'" + (bool) table2.Rows[0]["proisp"] + "'", ",'", re.replace(table2.Rows[0]["prokey"].ToString()), "','", re.replace(table2.Rows[0]["promsg"].ToString()), "','", re.replace(table2.Rows[0]["promark"].ToString()), "','", re.replace(table2.Rows[0]["prosize"].ToString()), "','", re.replace(table2.Rows[0]["procolor"].ToString()), "',", "'" + (bool) table2.Rows[0]["visible"] + "'", 
                        ",'", re.replace(table2.Rows[0]["pronameen"].ToString()), "','", re.replace(table2.Rows[0]["procontenten"].ToString()), "','", re.replace(table2.Rows[0]["updatetime"].ToString()), "',", table2.Rows[0]["languages"].ToString(), ",", table2.Rows[0]["clicks"].ToString(), ",", table2.Rows[0]["proorder"].ToString(), ")"
                     }));
                }
                base.Response.Write("<script language='javascript'>alert('复制成功！');window.location.href='goods.aspx';</script>");
                base.Response.End();
            }
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
        this.GridViewBind(string.Empty);
    }

    void BindProfessionAreaType()
    {
        DataSet ds;
        ds = theProfessionAreaType.GetList(" 1= 1");
        this.ddlProfessionAreaType.DataTextField = "Name";
        this.ddlProfessionAreaType.DataValueField = "Id";
        this.ddlProfessionAreaType.DataSource = ds;
        this.ddlProfessionAreaType.DataBind();
        this.ddlProfessionAreaType.Items.Insert(0, new ListItem("所属专区", ""));
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
        {
            CheckBox box = new CheckBox();
            box = (CheckBox) e.Row.FindControl("CheckBox1");
            HyperLink link = (HyperLink) e.Row.FindControl("HyperLink1");
            if (box != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string sKeyId = this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                    link.NavigateUrl = fun.geturl1("") + "products_details.aspx?pid=" + sKeyId;
                    e.Row.Cells[1].Width = 150;
                    if (e.Row.Cells[5].Text == "True")
                    {
                        e.Row.Cells[5].Text = "<a href=goods.aspx?action=yes1&amp;pid=" + sKeyId + "><img src='../images/yes.gif' border='0' /></a>";
                    }
                    else
                    {
                        e.Row.Cells[5].Text = "<a href=goods.aspx?action=no1&amp;pid=" + sKeyId + "><img src='../images/no.gif' border='0' /></a>";
                    }
                    if (e.Row.Cells[6].Text == "True")
                    {
                        e.Row.Cells[6].Text = "<a href=goods.aspx?action=yes2&amp;pid=" + sKeyId + "><img src='../images/yes.gif' border='0' /></a>";
                    }
                    else
                    {
                        e.Row.Cells[6].Text = "<a href=goods.aspx?action=no2&amp;pid=" + sKeyId + "><img src='../images/no.gif' border='0' /></a>";
                    }
                    if (e.Row.Cells[7].Text == "True")
                    {
                        e.Row.Cells[7].Text = "<a href=goods.aspx?action=yes3&amp;pid=" + sKeyId + "><img src='../images/yes.gif' border='0' /></a>";
                    }
                    else
                    {
                        e.Row.Cells[7].Text = "<a href=goods.aspx?action=no3&amp;pid=" + sKeyId + "><img src='../images/no.gif' border='0' /></a>";
                    }
                    if (e.Row.Cells[8].Text == "True")
                    {
                        e.Row.Cells[8].Text = "<a href=goods.aspx?action=yes4&amp;pid=" + sKeyId + "><img src='../images/yes.gif' border='0' /></a>";
                    }
                    else
                    {
                        e.Row.Cells[8].Text = "<a href=goods.aspx?action=no4&amp;pid=" + sKeyId + "><img src='../images/no.gif' border='0' /></a>";
                    }
                    if (e.Row.Cells[9].Text == "True")
                    {
                        e.Row.Cells[9].Text = "<a href=goods.aspx?action=yes5&amp;pid=" + sKeyId + "><img src='../images/yes.gif' border='0' /></a>";
                    }
                    else
                    {
                        e.Row.Cells[9].Text = "<a href=goods.aspx?action=no5&amp;pid=" + sKeyId + "><img src='../images/no.gif' border='0' /></a>";
                    }
                    e.Row.Cells[4].Text = info.info1("", "shophuobi") + e.Row.Cells[4].Text;
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                }
                else if (e.Row.RowType == DataControlRowType.Header)
                {
                    box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    //e.Row.Cells[6].Text = fun.shuname("1");
                    //e.Row.Cells[7].Text = fun.shuname("2");
                    //e.Row.Cells[8].Text = fun.shuname("3");
                    //e.Row.Cells[9].Text = fun.shuname("4");
                }

            }
        }
    }


    string GetSql()
    {
        string sql = "select *,isnull((select sum(isnull(g.goodsInCount,0) - isnull(g.goodsOutCount,0)) from tbGoodsInOut g where g.goodsId = DB_Shoppro.proid),0) as realyCount,"
                   + " (select isnull(sum(frozenCount),0) as frozenCount from tbGoodsFrozen where goodsId = DB_Shoppro.proid) as frozenCount from DB_Shoppro where visible=1 ";
        if (this.ishot.Value.ToString() == "1")
        {
            sql = sql + " and proisnew1=1";
        }
        if (this.ishot.Value.ToString() == "2")
        {
            sql = sql + " and proisnew2=1";
        }
        if (this.ishot.Value.ToString() == "3")
        {
            sql = sql + " and proisnew3=1";
        }
        if (this.ishot.Value.ToString() == "4")
        {
            sql = sql + " and proisnew4=1";
        }
        if (this.typeid.SelectedValue.ToString() != "0")
        {
            sql = sql + " and typeid in(" + re.returntype(this.typeid.SelectedValue.ToString(), this.typeid.SelectedValue.ToString(), info.languages("")) + ")";
        }
        if (this.propinpai.SelectedValue.ToString() != "0")
        {
            sql = sql + " and propinpai=" + this.propinpai.SelectedValue.ToString() + "";
        }
        if (re.replace(this.keyname.Value.ToString()) != "")
        {
            sql = sql + " and (proname like '%" + re.replace(this.keyname.Value.ToString()) + "%' or procode like '%" + re.replace(this.keyname.Value.ToString()) + "%')";
        }
        if (this.ddlProfessionAreaType.SelectedItem.Value != "")
        {
            sql = sql + " and ProfessionAreaTypeId = " + this.ddlProfessionAreaType.SelectedItem.Value;
        }

        return sql;
    }

    private void GridViewBind(string sOrderBy)
    {
        string sql = GetSql();
        DataTable table = new DataTable();
        table = SQLAccess.GreatDs(sql).Tables[0];
        table.DefaultView.Sort = sOrderBy == string.Empty ? " procode Asc " : sOrderBy;
        this.GridView1.DataSource = table.DefaultView;
        this.GridView1.DataBind();
        this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
        this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
        this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
        if (table.Rows.Count == 0)
        {
            this.btnFirst.Visible = false;
            this.btnPrev.Visible = false;
            this.btnNext.Visible = false;
            this.btnLast.Visible = false;
            this.LblCurrentIndex.Visible = false;
            this.LblPageCount.Visible = false;
            this.LblRecordCount.Visible = false;
        }
        else if (this.GridView1.PageCount == 1)
        {
            this.btnFirst.Visible = false;
            this.btnPrev.Visible = false;
            this.btnNext.Visible = false;
            this.btnLast.Visible = false;
        }
        this.btnFirst.CommandName = "1";
        this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
        this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
        this.btnLast.CommandName = this.GridView1.PageCount.ToString();
        this.ddlCurrentPage.Items.Clear();
        if (this.GridView1.PageCount > 0)
        {
            for (int i = 1; i <= this.GridView1.PageCount; i++)
            {
                this.ddlCurrentPage.Items.Add(i.ToString());
            }
            this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        this.GridViewBind(string.Empty);
    }


    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!base.IsPostBack)
        {
            base.ValidateLoginRule(ERule.ProductList);
            this.BindProfessionAreaType();
            this.GridViewBind(string.Empty);
            this.Bind();
        }
    }

    protected void PagerButtonClick(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = Convert.ToInt32(((LinkButton) sender).CommandName) - 1;
        this.GridViewBind(string.Empty);
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["Ascending"] == null)
        {
            ViewState["Ascending"] = " desc ";
            this.GridViewBind(e.SortExpression + " asc ");
        }
        else
        {
            this.GridViewBind(e.SortExpression + ViewState["Ascending"].ToString());
            if (ViewState["Ascending"].ToString() == " asc ")
            {
                ViewState["Ascending"] = " desc ";
            }
            else
            {
                ViewState["Ascending"] = " asc ";
            }
        }

    }

  
    }
}